function Pager(tableName, itemsPerPage) {

	this.tableName = tableName;

	this.itemsPerPage = itemsPerPage;

	this.currentPage = 1;

	this.pages = 0;

	this.inited = false;

	this.showRecords = function(from, to) {

		var rows = document.getElementById(tableName).rows;

		// i starts from 1 to skip table header row

		for (var i = 1; i < rows.length; i++) {

			if (i < from || i > to)

				rows[i].style.display = 'none';

			else

				rows[i].style.display = '';

		}

	}

	this.showPage = function(pageNumber) {

		if (!this.inited) {

			alert("not inited");

			return;

		}

		var oldPageAnchor = document.getElementById('pg' + this.currentPage);

		this.currentPage = pageNumber;

		var newPageAnchor = document.getElementById('pg' + this.currentPage);

		var from = (pageNumber - 1) * itemsPerPage + 1;

		var to = from + itemsPerPage - 1;

		this.showRecords(from, to);

		var element = document.getElementById("currentPage");
		element.innerHTML = 'page ' + pageNumber + ' of ' + this.pages + '';

		if (pageNumber == 1) {
			document.getElementById("previousPage").style.display = 'none';
			document.getElementById("nextPage").style.display = '';
		} else if (pageNumber != 1 && pageNumber != this.pages) {
			document.getElementById("previousPage").style.display = '';
			document.getElementById("nextPage").style.display = '';
		} else {
			document.getElementById("previousPage").style.display = '';
			document.getElementById("nextPage").style.display = 'none';
		}

	}

	this.prev = function() {

		if (this.currentPage > 1)

			this.showPage(this.currentPage - 1);

	}

	this.next = function() {

		if (this.currentPage < this.pages) {

			this.showPage(this.currentPage + 1);

		}

	}

	this.first = function() {
		this.showPage(1);
	}

	this.last = function() {
		this.showPage(this.pages);
	}

	this.init = function() {

		var rows = document.getElementById(tableName).rows;

		var records = (rows.length - 1);

		this.pages = Math.ceil(records / itemsPerPage);

		this.inited = true;

	}

	this.showPageNav = function(pagerName, positionId) {

		if (!this.inited) {

			alert("not inited");

			return;

		}
		var element = document.getElementById(positionId);

		var pagerHtml = '';

		pagerHtml += '<ul>';
		pagerHtml += '<li><a id="firstPage" class="" onclick="' + pagerName
				+ '.first();">First</a></li>';
		pagerHtml += '<li><a id="previousPage" class="" onclick="' + pagerName
				+ '.prev();">Previous</a></li>';

		for (var page = 1; page <= this.pages; page++) {
			pagerHtml += '<li><a id="pg' + page + '" class="" onclick="'
					+ pagerName + '.showPage(' + page + ');">' + page
					+ '</a></li>';
		}

		pagerHtml += '<li><a id="nextPage" class="" onclick="' + pagerName
				+ '.next();">Next</a></li>';
		pagerHtml += '<li><a id="lastPage" class="" onclick="' + pagerName
				+ '.last();">Last</a></li>';
		pagerHtml += '</ul><br>';
		pagerHtml += '<div id=\"currentPage\"></div>';

		element.innerHTML = pagerHtml;
	}

}